package com.lg.cms.shiro.filter;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Description: 带指定重定向的认证过滤器
 * @Author: wuyuhang
 * @create: 2020-10-02 12:24
 */
public class LoginUriFilter extends FormAuthenticationFilter {
    private String userLoginUrl = "/login/";
    private String superUserLoginUrl = "/admin/login/";

    /**
     * 重写此方法来根据url动态改变登录url
     * @param request
     * @param response
     * @throws IOException
     */
    @Override
    protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String url = httpServletRequest.getRequestURI();
        if(url.contains("admin")){
            WebUtils.issueRedirect(request, response, superUserLoginUrl);
        }else{
            WebUtils.issueRedirect(request, response, userLoginUrl);
        }
    }
}